package gov.child.service.impl;

import gov.child.dao.ComplaintDao;
import gov.child.entities.po.EComplaint;
import gov.child.entities.po.EUser;
import gov.child.entities.vo.*;
import gov.child.service.ComplaintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author fzh
 * @since 2017/9/14
 */
@Service("complaintService")
public class ComplaintServiceImpl implements ComplaintService {

    @Autowired
    private ComplaintDao complaintDao;

    @Override
    public Complaint findById(Integer id) {
        EComplaint complaint = complaintDao.findById(id);
        if (Objects.isNull(complaint)) return Complaint.EMPTY_COMPLAINT;
        return Complaint.parseFrom(complaint);
    }

    @Override
    public List findByUser(User user) {
        EUser u = User.parseTo(user);
        List l = complaintDao.findByUser(u);
        return BeanTransformer.parseFrom(Complaint.class, l);
    }

    @Override
    public boolean save(Complaint complaint) {
        EComplaint c = Complaint.parseTo(complaint);
        complaintDao.save(c);
        return true;
    }

    @Override
    public boolean delete(Complaint complaint) {
        EComplaint c = Complaint.parseTo(complaint);
        complaintDao.delete(c);
        return true;
    }

    @Override
    public boolean update(Complaint complaint) {
        EComplaint c = Complaint.parseTo(complaint);
        complaintDao.update(c);
        return true;
    }

    @Override
    public Page getPage(int pageSize) {
        long size = complaintDao.total();
        return PageUtil.createPage(pageSize, size, 1);
    }
}
